package com.heima.admin.controller.v1;

import com.heima.admin.service.UserLoginService;
import com.heima.apis.admin.LoginControllerApi;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.utils.common.CookieUtil;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/login")
public class LoginControllerApiImpl implements LoginControllerApi {

    /**
     * 登录
     * @param dto
     * @return
     */
    @Autowired
    private UserLoginService userLoginService;

    @PostMapping("/in")
    @Override
    public ResponseResult login(@RequestBody AdUserDto dto) {
        return userLoginService.login(dto);
    }

    //使用redis和cookie 简化了前端传送token
    //@PostMapping("/in")
    @Override
    public ResponseResult login(AdUserDto dto, HttpServletResponse response) {
        CookieUtil.addCookie(response,"localhost","/","uid",dto.getName(),-1,false);
        return userLoginService.login(dto);
    }
}
